//
//  HttpHelperWithComment.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/27.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "HttpHelperWithComment.h"
#import "CommentModel.h"
#import "HomePageModel.h"
@interface HttpHelperWithComment ()
@property (nonatomic,strong) NSMutableArray *modelArray;
@end

@implementation HttpHelperWithComment

+(id)sharedHttpHelper
{
    static HttpHelperWithComment *handle=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle=[[HttpHelperWithComment alloc] init];
    });
    return handle;

}
//[NSString stringWithFormat:@"http://m2.qiushibaike.com/article/%ld/comments",self.ID]
//请求评论数据
-(void)fetchModelWithUrl:(NSString *)url block:(Block)block
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *Url =[NSURL URLWithString:url];
        NSURLSession *session=[NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask=[session dataTaskWithURL:Url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSDictionary *rootDic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *array=rootDic[@"items"];
            self.modelArray=[NSMutableArray array];
            for (NSDictionary *dic in array) {
                HomePageModel *model=[[HomePageModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                block();
            });
        }];
        [dataTask resume];
    });
}
//懒加载
-(NSMutableArray *)modelArray
{
    if (!_modelArray) {
        
        _modelArray = [NSMutableArray array];
        
    }
    
    return _modelArray;
}

//返回数组个数
-(NSInteger)countOfModelArray
{
    return _modelArray.count;
    
}


//根据下标返回对应的model
-(HomePageModel *)returnModelWithIndex:(NSInteger)index
{
    return _modelArray[index];
    
}


@end
